package proc

import (
	"github.com/hu17889/go_spider/core/common/page"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"log"
	"net/http"
	"strconv"
	"strings"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
	"time"
)

type ChouTiProcesser struct {
	ChouTiService    service.ChouTiService
	SpiderUrlONE string
	SpiderUrlTWO string
	cookies []*http.Cookie
}

func GetChouTiProcesser() *ChouTiProcesser {
	return &ChouTiProcesser{}
}

func (this *ChouTiProcesser) Startup() {
	newSpider := spider.NewSpider(this, "ChouTiProcesser")

	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	fullUrlOne := strings.Replace(this.SpiderUrlONE, "${specNum}", strconv.FormatInt(time.Now().Unix(), 10) , 1)
	fullUrlTwo := strings.Replace(this.SpiderUrlTWO, "${specNum1}", strconv.FormatInt(time.Now().Unix(), 10), 1)
	fullUrlTwo = strings.Replace(fullUrlTwo, "${specNum2}", strconv.FormatInt(time.Now().Unix(), 10), 1)
	base.Log.Info("start URL:,", fullUrlOne)
	base.Log.Info("start URL:,", fullUrlTwo)
	newSpider = newSpider.AddUrl(fullUrlOne, "json")
	newSpider = newSpider.AddUrl(fullUrlTwo, "json")

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(1).Run()
}

func (this *ChouTiProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	var pagenuma int
	if len(request.Url)> 48 {
		pagenuma = 60
	}else{
		pagenuma =30
	}

	query := p.GetJson()
	status, err := query.Get("code").Int()
	if status != 200 || err != nil {
		log.Panicf("page is crawled error : errorinfo=%s : status=%d : startNewsId=%d", err.Error(), status, 1)
	}
	level := enums.GetListLevel("抽屉")
	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	query = query.Get("data")
	for i := 0; i < pagenuma; i++ {
		idi, err := query.GetIndex(i).Get("id").Int()
		if err != nil {
		}

		title, err := query.GetIndex(i).Get("title").String()
		if title == "" || err != nil {
		}
		img_url, err := query.GetIndex(i).Get("img_url").String()
		if err != nil {

		}
		originalUrl, err := query.GetIndex(i).Get("originalUrl").String()
		if err != nil {

		}
		nick, err := query.GetIndex(i).Get("submitted_user").Get("nick").String()
		if err != nil {

		}

		if len(title) >3 {
			zhi := new(pojo.List)
			zhi.ListId = strconv.Itoa(idi)
			zhi.Image = img_url
			zhi.Author = nick
			zhi.Content = ""
			zhi.Title = title
			zhi.SUrl = originalUrl
			if len(zhi.SUrl) > 0 {
				//设置朝代
				zhi.Type = level
				exist := this.ChouTiService.Exist(zhi)
				if !exist {
					zhi_list_slice = append(zhi_list_slice, zhi)
				} else {
					zhi_list_update_slice = append(zhi_list_update_slice, zhi)
				}
			}
		}

	}

	//执行入库
	this.ChouTiService.SaveList(zhi_list_slice)
	this.ChouTiService.ModifyList(zhi_list_update_slice)

}

func (this *ChouTiProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}

